<template>
    <view class="cal-content" :class="{'cal-content2':type==2}">
        <cu-custom :isBack="true" :isWhiteBack="true" >
            <block slot="backText"></block>
            <block slot="content">
                <view class="custom-title">{{title}}</view>
            </block>
        </cu-custom>
        <view class="edit_top" :class="{'edit_top2':type==2}">
            <view class="tops">
                <view>发起单位：{{detailInfo.unit}}</view>
                <view>ID:{{detailInfo.code}}</view>
            </view>
            <view class="tops_title">{{detailInfo.title}}</view>
            <view class="paratext">{{detailInfo.subtitle}}</view>
            <view v-if="type==2" class="paratext_titmes">截止时间: {{detailInfo.end_time_text}}</view>
			<view class="tops_tip" v-if="type==1 && detailInfo.can_help == 1">
				<view class="tops_tip_left">
                    <button open-type="share" v-if="detailInfo.help_user_id == 0" class="lefts"><image class="share_icon" src="https://txr001.zthj.net/voucher_img/share_icon.png"></image>授权他人协助打卡</button>
					<view v-if="detailInfo.help_user_id == 0" class="wenicons" @click="istip=!istip" :class="{'weniconsicon':istip}">
						<image src="https://txr001.zthj.net/voucher_img/wen_icon.png"></image>
						<view class="tip_box" v-if="istip">只能授权一个用户，授权后可以取消</view>
					</view>


                    <view v-if="detailInfo.help_user_id > 0" class="lefts">
                        <image class="share_icon" src="https://txr001.zthj.net/voucher_img/share_icon2.png"></image>
                        已授权给:<image class="momo" :src="detailInfo.help_user.avatar"></image>{{detailInfo.help_user.nickname}}</view>

                </view>
				<view @click="unhelp" v-if="detailInfo.help_user_id > 0 && detailInfo.log_user_id == vuex_user.id" class="tops_tip_rgiht">取消授权</view>
			</view>
			<view class="tops_tip" v-if="type==2">
				<view class="tops_tip_left">
					<button class="not-show-but" type="default" open-type="share"></button>
					<view class="lefts"><image class="share_icon" src="https://txr001.zthj.net/voucher_img/share_icon.png"></image>分享</view>
				</view>
			</view>
            <view class="timesbox" v-if="type==1">
                <view class="time_title">
                    <view>任务周期</view>
                    <view>{{detailInfo.day}}/{{detailInfo.days}}</view>
                </view>
                <view class="clock_box">
                    <view class="clock_box_top">
                        <view class="clock_left">打卡时间: {{detailInfo.daka}}</view>
                        <view class="clock_right">
                            <view><text></text>已打卡</view>
                            <view><text></text>有逾期</view>
                        </view>
                    </view>
                    <view class="selectedbox">
                        <uni-calendar class="uni-calendar--hook"
                                      :selected="selected"
                                      :showMonth="false"
                                      :start-date="detailInfo.start_date_text"
                                      :end-date="detailInfo.end_date_text"
                                      @change="change"
                                      @monthSwitch="monthSwitch" />
                    </view>
                </view>
            </view>
        </view>
        <view class="edit_mian_box" v-if="type==1">
            <view class="tablist">
                <view @click="tabsfn(index)" v-for="(itme,index) in tabs" :key="index" :class="{'okview':index==tabsindex}">{{itme.title}}</view>
            </view>
            <view class="edit_mian" v-if="tabsindex==0">

                <view v-if="answer == null">
                    <preview-form ref="preview" :dataList="list" v-if="list.length > 0"
                                  :default_user_index="default_user_index"></preview-form>
                </view>
                <view v-else>
                    <show-res-form ref="show_res_form"
                                   :dataList="list"
                                   @resultDataSave="resDataSave"
                                   :resultData="resData"
                    ></show-res-form>
                </view>


                <view v-if="!answer && onlyread != 1" @click="submit" class="formbnt">立即打卡</view>
            </view>
            <view class="edit_mian"  v-if="tabsindex==1">
                <view class="completed">
                    <view class="completed_title" @click="nocompleted=!nocompleted">
                        <view class="titles">逾期(未完成)</view>
                        <view class="titles_rgiht" ><text>{{myJoinLog.expired.length}}天</text><image :class="{'titles_rgihtok':!nocompleted,'titles_rgihtno':nocompleted}" src="https://txr001.zthj.net/voucher_img/top_icon.png"></image></view>
                    </view>
                    <view class="main_list" v-if="nocompleted">
                        <crewListMyself :list="myJoinLog.expired" :ispersonage="ispersonage" @jumpPersonage="jumpPersonage"></crewListMyself>
                    </view>
                </view>
                <view class="completed">
                    <view class="completed_title" @click="nocompleted1=!nocompleted1">
                        <view class="titles">已完成</view>
                        <view class="titles_rgiht" ><text>{{myJoinLog.finish.length}}天</text>
                            <image :class="{'titles_rgihtok':!nocompleted1,'titles_rgihtno':nocompleted1}" src="https://txr001.zthj.net/voucher_img/top_icon.png"></image>
                        </view>
                    </view>
                    <view class="main_list" v-if="nocompleted1">
                        <crewList :list="myJoinLog.finish" :ispersonage="ispersonage" @jumpPersonage="jumpPersonage"></crewList>
                    </view>
                </view>
            </view>
        </view>
        <view class="edit_mian_box"  v-if="type==2" :class="{'edit_mian_box2':type==2}">
            <view class="edit_mian">

                <preview-form ref="preview" :dataList="list" v-if="list.length > 0"
                              :default_user_index="default_user_index"></preview-form>
                <view @click="submit" class="formbnt">立即提交</view>
            </view>
        </view>
    </view>
</template>

<script>
import crewList from '../../components/voucher-component/crew_list'
import crewListMyself from '../../components/voucher-component/crew_list_myself'
import previewForm from "../../components/diy-form/preview-form.vue";
import showResForm from "@/subpackage/components/diy-form/show-res-form.vue"; //列表
export default {
    components: {
        showResForm,
        previewForm,
        crewListMyself,
        crewList
    },
    data() {
        return {
            onlyread: 0,
            s_id: 0,
            help_id:0,
            list:[],
            default_user_index: -1,
            detailInfo:{},
            activeDate: '',
            answer: null,
            resData:[],
            myJoinLog:{
                expired: [],
                finish: [],
            },
            istip:false,
            nocompleted:true,
            nocompleted1:true,
            selected:[],
            borderbottom:false,
            labelPosition:"top",
            title:"",
            form:{},
            scrollHeight:0,
            personallist:[{type:1},{type:1},{type:2}],
            personallist1:[{type:1},{type:1},{type:2},{type:1},{type:1},{type:2}],
            tabs:[
                {title:"打卡内容"},
                {title:"打卡记录"}
            ],
            crewArr:[{type:1},{type:1},{type:2}],
            tabsindex:0,
            type:1
        }
    },
	//分享
	onShareAppMessage() {
        if(this.detailInfo.type == 1){
            //问卷
            return {
                title: '邀请您参与:' + this.detailInfo.title,
                path: `/pages/index/questionGrant?s_id=` + this.detailInfo.s_id,
            }
        }else if(this.detailInfo.type == 2 && this.detailInfo.help_user_id == 0){
            //授权打卡

            return {
                title: '邀请您协助打卡参与:' + this.detailInfo.title,
                path: `/pages/index/clockGrant?s_id=` + this.detailInfo.s_id + "&help_id=" + this.detailInfo.log_id,
            }

        }

	},
    onLoad(res) {
        console.log(res)
        let that=this
        this.type=res.type
        this.title=res.type==1?'打卡':"问卷"
        this.s_id = res.id;
        this.help_id = res.help_id || 0;
        this.onlyread = res.onlyread || 0;
        this.load();
    },
    methods:{
        async load(){
            var that = this;
            if (that.s_id > 0) {
                let res = await that.$api.getStatisticsDetailInfo({
                    s_id: that.s_id,
                    help_id: that.help_id,
                    active_date : that.activeDate
                });
                if (res.code == 1) {
                    // 日历标记
                    if(res.data.calendar.length > 0){
                        that.selected = res.data.calendar;
                    }
                    that.answer = res.data.answer
                    if(res.data.answer != null){
                        //已经答题了
                        that.resData = JSON.parse(res.data.answer.content)
                    }



                    var detailInfo = res.data.info;
                    that.detailInfo = detailInfo;
                    if (detailInfo.join_datalist) {
                        that.list = JSON.parse(detailInfo.join_datalist)
                    }
                    if (detailInfo.last_content) {
                        setTimeout(function() {
                            var contentArr = JSON.parse(detailInfo.last_content);
                            that.$refs.preview.saveInitResultData(contentArr)
                        }, 100)
                    }
                    that.showSubmit = true;
                } else {
                    that.msg(res.msg);
                    setTimeout(function(){
                        uni.navigateBack();
                    }, 1000)
                }

            }
        },
        change(e){
			console.log('点击日历')
			return;
            // let today = new Date();
            // let date = new Date(e.fulldate + " 00:00:00");
            // if (date > today) {
            //     uni.showToast({icon:"none", title:'日期不能大于今天'});
            //     return;
            // }

            // this.activeDate = e.fulldate
            // this.load();
        },
        monthSwitch(){

        },
        async submit() {
            var that = this;
            // var checkLogin = that.checkLogin(true);
            // if (checkLogin == false) {
            // 	that.navigateTo('/pages/login/selLogin');
            // 	return;
            // }
            //this.$refs.preview.uploadFile(); //检测上传文件
            that.showModal2('确认参加', '', async function(res) {
                if (res == 'yes') {
                    if (that.detailInfo.join_datalist != '') {
                        var resData = await that.$refs.preview.getFormData(); //获取最终数据方法

                        // console.log('join resData',resData);
                        if (!resData || resData.length < 1) {
                            return false;
                        }
                    } else {
                        var resData = '';
                    }
                    // 数组转换为字符串的一种方法是在其后连接一个空字符串
                    // var need_check_text = resData + "";
                    // if (need_check_text) {
                    // 	var res = await that.checkQiniuText(need_check_text);
                    // 	console.log("校验结果" + JSON.stringify(res))
                    // 	if (!res.status) {
                    // 		return;
                    // 	}
                    // }


                    that.loadingMsg('正在提交数据');
                    let result = await that.$api.joinStatisticsNew({
                        resData,
                        s_id: that.s_id,
                        help_id: that.help_id,
                        active_date: that.activeDate
                    });

                    that.msg(result.msg)
                    if (result.code == 1) {
                       if(that.detailInfo.type == 2){
                           that.load()
                       }else{
                           uni.navigateBack();
                       }
                    }
                }
            })
            //var check = this.$refs.preview.checkFormData();  //检测填写数据是否完善
            //console.log(resData)
        },
        toPages(item){
            uni.navigateTo({
                url:`/subpackage/pages/voucher/${item}`
            })
        },
        tabsfn(index){
            this.tabsindex=index
            if(index == 1){
                this.getMyJoinLog();
            }
        },
        async getMyJoinLog(){
            let that = this;
            let result = await that.$api.getMyJoinLog({
                s_id: that.s_id,
                help_id : that.help_id
            });
            that.myJoinLog = result.data.list
        },
        jumpPersonage(res){
            console.log(res);

            if(res.join_id == 0){
                // 去补卡界面
                let today = new Date();
                let date = new Date(res.date + " 00:00:00");
                if (date > today) {
                    uni.showToast({icon:"none", title:'日期不能大于今天'});
                    return;
                }

                this.activeDate = res.date
                this.tabsindex=0;
                this.load();


                return;
            }
            uni.navigateTo({
                url:"/subpackage/pages/voucher/completeClock?type="+res.type + "&join_id="+res.join_id
            })
        },
        resDataSave(saveData){
            this.resData = saveData;
        },
        unhelp(){
            let that = this;
            uni.showModal({
                title: '提醒',
                content: '是否确认取消协助？',
                success:  async function(ret) {
                    if (ret.confirm) {
                        let res = await that.$api.unhelpDaka({
                            help_id : that.detailInfo.log_id,
                        })
                        if(res.code == 1){
                            that.load();
                        }else{
                            uni.showModal({content:res.msg,showCancel:false})
                            return;
                        }
                    }
                }
            });

        },

    }
}
</script>

<style lang="less" scoped>
.cal-content{
    /deep/.cu-bar{
        background-color: #1E9D6A;
    }
}
.cal-content2{
    /deep/.cu-bar{
        background-color: #4554C2;
    }
}
.custom-title{
    font-size: 34rpx;
    font-family: PingFang SC, PingFang SC;
    font-weight: 600;
    color: #FFFFFF;
}
.edit_top{
    padding-bottom: 200rpx;
    padding-top: 32rpx;
    background: #1E9D6A;
    border-radius: 0rpx 0rpx 0rpx 0rpx;
    opacity: 1;
    padding-left: 32rpx;
	position: relative;

    .tops{
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding-right: 32rpx;
        view{
            font-size: 24rpx;
            font-family: PingFang SC, PingFang SC;
            font-weight: 400;
            color: #FFFFFF;
            line-height: 28rpx;
            margin-bottom: 24rpx;
        }
    }
    .tops_title{
        font-size: 40rpx;
        font-family: PingFang SC, PingFang SC;
        font-weight: 600;
        color: #FFFFFF;
        line-height: 47rpx;
        margin-bottom: 8rpx;
    }
    .paratext{
        font-size: 24rpx;
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        color: #FFFFFF;
        line-height: 32rpx;
        margin-bottom: 32rpx;
        padding-right: 32rpx;
    }
    .paratext_titmes{
        height: 34rpx;
        font-size: 24rpx;
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        color: #FFFFFF;
        line-height: 28rpx;
        margin-bottom: 24rpx;
    }
    .tops_tip{
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-bottom: 24rpx;
        padding-right: 32rpx;
        .tops_tip_left{
            display: flex;
            align-items: center;
            position: relative;
			.not-show-but{
				position: absolute;
				top: 0;
				left: 0;
				width: 100%;
				height: 100%;
				margin: 0;
				opacity: 0;
				padding: 0;
			}
            .lefts{
                padding: 12rpx 16rpx;
                background: #FFFFFF;
                border-radius: 27rpx 27rpx 27rpx 27rpx;
                display: inline-flex;
                align-items: center;
                font-size: 24rpx;
                font-family: PingFang SC, PingFang SC;
                font-weight: 400;
                color: #24458E;
                .share_icon{
                    width: 32rpx;
                    height: 32rpx;
                    display: block;
                    margin-right: 8rpx;
                }
                .momo{
                    width: 40rpx;
                    height: 40rpx;
                    background: #D9DDDF;
                    border-radius: 100%;
                    margin-right: 8rpx;
                    margin-left: 8rpx;
                }
            }
            button.lefts{
                height:68rpx;
            }
            .wenicons{
                width: 32rpx;
                height: 32rpx;
                margin-left: 32rpx;
                position: relative;
                image{
                    width: 100%;
                    height: 100%;
                    display: block;
                }
                .tip_box{
                    position: absolute;
                    left: 50%;
                    transform: translateX(-50%);
                    top: 57rpx;
                    width: 362rpx;
                    height: 60rpx;
                    text-align: center;
                    line-height: 60rpx;
                    background: #767879;
                    border-radius: 16rpx 16rpx 16rpx 16rpx;
                    opacity: 0.9;
                    font-size: 20rpx;
                    font-family: PingFang SC, PingFang SC;
                    font-weight: 400;
                    color: #FFFFFF;
					z-index: 99;
                }

            }
            .weniconsicon{
                position: relative;
                &:before{
                    content: "";
                    width: 0rpx;
                    height: 0rpx;
                    opacity: 0.9;
                    border: 20rpx solid;
                    border-color: transparent transparent #767879 transparent;
                    position: absolute;
                    left: 50%;
                    bottom: -25rpx;
                    transform: translateX(-50%);
                }
            }
        }
        .tops_tip_rgiht{
            font-size: 24rpx;
            font-family: PingFang SC, PingFang SC;
            font-weight: 400;
            color: #FFFFFF;
            text-decoration: underline;
        }
    }
    .timesbox{
		position: absolute;
        width: 686rpx;
        background: #FFFFFF;
        border-radius: 48rpx 48rpx 48rpx 48rpx;
        .time_title{
            padding: 32rpx;
            padding-bottom: 16rpx;
            border-bottom: 2rpx solid #D9D9D9;
            display: flex;
            align-items: center;
            justify-content: space-between;
            view{
                &:first-child{
                    font-size: 28rpx;
                    font-family: PingFang SC, PingFang SC;
                    font-weight: 400;
                    color: #010101;
                    line-height: 33rpx;
                }
                &:last-child{
                    padding: 0 8rpx;
                    height: 32rpx;
                    line-height: 32rpx;
                    background: #0FBC77;
                    border-radius: 16rpx 16rpx 16rpx 16rpx;
                    font-size: 20rpx;
                    font-family: PingFang SC, PingFang SC;
                    font-weight: 600;
                    color: #FFFFFF;
                }
            }
        }
        .clock_box{
            padding: 24rpx 32rpx;
            padding-bottom: 36rpx;
            .clock_box_top{
                display: flex;
                align-items: center;
                justify-content: space-between;
                .clock_left{
                    font-size: 24rpx;
                    font-family: PingFang SC, PingFang SC;
                    font-weight: 400;
                    color: #010101;
                    line-height: 28rpx;
                }
                .clock_right{
                    display: flex;
                    align-items: center;
                    view{
                        font-size: 20rpx;
                        font-family: PingFang SC, PingFang SC;
                        font-weight: 400;
                        color: #010101;
                        line-height: 23rpx;
                        margin-left: 32rpx;
                        text{
                            width: 8rpx;
                            height: 8rpx;
                            margin-right: 18rpx;
                            border-radius: 100%;
                            display: inline-block;
                            vertical-align: middle;
                        }
                        &:first-child{
                            text{
                                background-color: #0FBC77;
                            }
                        }
                        &:last-child{
                            text{
                                background-color: #EC6969;
                            }
                        }
                    }
                }
            }
        }
    }
}
.edit_top2{
    background: #4554C2;
    padding-bottom: 178rpx;
}
.edit_mian_box{
    padding: 32rpx;
    margin-top: 665rpx;
	position: relative;
}
.edit_mian_box2{
    margin-top: -170rpx;
	position: relative;
}
.tablist{
    padding: 48rpx 144rpx;
    padding-bottom: 58rpx;
    display: flex;
    align-items: center;
    justify-content: space-between;
    view{
        font-size: 32rpx;
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        color: #010101;
        line-height: 38rpx;
        position: relative;
    }
    .okview{
        position: relative;
        &::after{
            content: "";
            position: absolute;
            bottom: -28rpx;
            left: 50%;
            transform: translateX(-50%);
            width: 64rpx;
            height: 8rpx;
            background: #010101;
            border-radius: 0rpx 0rpx 0rpx 0rpx;
        }
    }
}
.edit_mian{
    padding: 32rpx;
    background: #FFFFFF;
    border-radius: 48rpx 48rpx 48rpx 48rpx;
    .formbnt{
        width: 454rpx;
        height: 88rpx;
        background: #24458E;
        border-radius: 44rpx 44rpx 44rpx 44rpx;
        text-align: center;
        line-height: 88rpx;
        font-size: 32rpx;
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        color: #FFFFFF;
        margin: 0 auto;
    }
    .completed{
        background: #FFFFFF;
        border-radius: 48rpx 48rpx 48rpx 48rpx;
        padding: 0 32rpx;
        margin-bottom: 32rpx;
        .completed_title{
            padding-bottom: 8rpx;
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 2rpx solid #D9D9D9;
            .titles{
                font-size: 28rpx;
                font-family: PingFang SC, PingFang SC;
                font-weight: 600;
                color: #010101;
                line-height: 50rpx;
            }
            .titles_rgiht{
                position: relative;
                display: flex;
                text{
                    font-size: 24rpx;
                    font-family: PingFang SC, PingFang SC;
                    font-weight: 400;
                    color: #767879;
                    line-height: 28rpx;
                    margin-right: 8rpx;
                }
                image{
                    width: 32rpx;
                    height: 32rpx;
                    display: flex;
                }
            }
            .titles_rgihtok{
                animation: rotation 0.5s  forwards;
            }
            .titles_rgihtno{
                animation: rotatiyew 0.5s  forwards;
            }

            @keyframes rotation {
                0% {
                    transform: rotate(0deg);
                }
                100% {
                    transform: rotate(180deg);
                }
            }
            @keyframes rotatiyew {
                0% {
                    transform: rotate(180deg);
                }
                100% {
                    transform: rotate(0deg);
                }
            }
        }
        .main_list{
            padding-top: 32rpx;
        }
    }
}
</style>